﻿using Microsoft.Win32;
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace ET.Score.SharedLibrary.Wcf
{
    public class MotherboardInfo
    {
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool GetVolumeInformation(string lpRootPathName, StringBuilder lpVolumeNameBuffer, int nVolumeNameSize,
                                                         out uint lpVolumeSerialNumber, out uint lpMaximumComponentLength, out uint lpFileSystemFlags,
                                                         StringBuilder lpFileSystemNameBuffer, int nFileSystemNameSize);

        public static string GetMotherboardSerial()
        {
            string machineGuid = null;
            try
            {
                RegistryKey registryKey = Registry.CurrentUser;
                if (registryKey != null)
                {
                    RegistryKey srk = registryKey.OpenSubKey(@"BJDX", true);

                    // 如果Software键不存在，OpenSubKey将返回null，此时需要创建它  
                    if (srk == null)
                    {
                        srk = Registry.CurrentUser.CreateSubKey("BJDX");
                    }

                    if (srk != null)
                    {
                        //注册表号(机器号)
                        var signalInstructorNumber = srk.GetValue(@"IdentificationNumber");
                        if (signalInstructorNumber != null)
                        {
                            machineGuid = signalInstructorNumber.ToString();
                        }
                        else
                        {
                            machineGuid = GetRandomCharacters(16);
                            srk.SetValue("IdentificationNumber", machineGuid);
                        }
                    }

                }
                registryKey.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred: " + ex.Message);
            }
            return machineGuid;
        }

        public static string GetRandomCharacters(int n = 10, bool Number = true, bool Lowercase = true, bool Capital = true)  // 生成随机字符串
        {
            StringBuilder tmp = new StringBuilder();
            Random rand = new Random();
            string characters = (Capital ? "ABCDEFGHIJKLMNOPQRSTUVWXYZ" : null) + (Number ? "0123456789" : null) + (Lowercase ? "abcdefghijklmnopqrstuvwxyz" : null);
            if (characters.Length < 1)
            {
                return (null);
            }
            for (int i = 0; i < n; i++)
            {
                tmp.Append(characters[rand.Next(0, characters.Length)].ToString());
            }
            return (tmp.ToString());
        }


        //public static string GetMachineGuid()
        //{
        //    string machineGuid = null;
        //    try
        //    {
        //        RegistryKey registryKey = Registry.LocalMachine;
        //        if (registryKey != null)
        //        {
        //            RegistryKey srk = registryKey.OpenSubKey(@"SOFTWARE\Microsoft\Cryptography", true);
        //            if (srk != null)
        //            {
        //                //注册表号(机器号)
        //                var signalInstructorNumber = srk.GetValue(@"MachineGuid");
        //                if (signalInstructorNumber != null)
        //                {
        //                    machineGuid = signalInstructorNumber.ToString();
        //                }
        //            }
        //        }
        //        registryKey.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine("An error occurred: " + ex.Message);
        //    }
        //    return machineGuid;
        //}
    }
}
